package com.xinshujia.wxxcx.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.xinshujia.wxxcx.pojos.info.AlarmRecordCause;
import com.xinshujia.wxxcx.pojos.info.AlarmRecordNumInfo;
import com.xinshujia.wxxcx.service.AlarmRecordService;
import com.xinshujia.wxxcx.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Cui Han
 * @Date 2024/5/17 15:38
 * @Description: TODO
 * @Version 1.0
 */
@RestController
@RequestMapping("alarmRecord")
public class AlarmRecordController {

    @Autowired
    private AlarmRecordService alarmRecordService;


    /**
     * 查询报警列表
     *
     * @return
     */
    @GetMapping("findAlarmRecord")
    @SaCheckLogin
    public Result findAlarmRecord() {
        return Result.ok(alarmRecordService.findAlarmRecord());
    }

    /**
     * 报警详情
     * @param id
     * @return
     */
    @GetMapping("findByIdAlarmCause")
    @SaCheckLogin
    public Result findByIdAlarmCause(Integer id){
        return Result.ok(alarmRecordService.findByIdAlarmCause(id));
    }


    /**
     * 修改报警详情
     *
     * @param
     * @return
     */
    @PostMapping("/updateAlarmCause")
    @SaCheckLogin
    public Result updateAlarmCause(@RequestBody AlarmRecordCause alarmRecordCause){
        return alarmRecordService.updateAlarmCause(alarmRecordCause);
    }


    /**
     * 查询机床报警排名
     * @return
     */
    @GetMapping("/findAlarmRecordNum")
    @SaCheckLogin
    public Result findAlarmRecordNumFormatted() {
        List<AlarmRecordNumInfo> data = alarmRecordService.findAlarmRecordNum();
        List<List<Object>> formattedData = new ArrayList<>();
        for (AlarmRecordNumInfo info : data) {
            List<Object> record = new ArrayList<>();
            record.add(info.getMachineName());
            record.add(info.getNum());
            formattedData.add(record);
        }
        return Result.ok(formattedData) ;
    }


    /**
     * 新增报警记录
     */
    @Scheduled(fixedRate=1000)
    //@GetMapping("insertAlarmRecord")
    public void insertAlarmRecord(){
        alarmRecordService.insertAlarmRecord();
    }

    /**
     * 报警弹窗
     * @return
     */
    @GetMapping("/findAlarm")
    @SaCheckLogin
    public Result findAlarm(){
        return Result.ok(alarmRecordService.findAlarm());
    }
}
